﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class box : MonoBehaviour
{
    private NavMeshAgent myAgent;
    static int fdServerSocket = -1;
    // Start is called before the first frame update
    void Start()
    {
        myAgent = gameObject.GetComponent<NavMeshAgent>();
        Assets.ClientNetwork.actionOnConnectResult = OnConnectResult;
        Assets.ClientNetwork.actionOnRecv = OnRecv;
    }

    private void OnRecv(ushort main,ushort sub,byte[] buf, int size)
    {
        var login = Assets.ClientNetwork.parseMsg<TestSvr.Login>(buf, size);
        if( null == login )
        {
            Debug.LogError("ParseFrom");
            return;
        }

        Debug.Log( main + "," + sub + "login,fd=" + login.ServerFd );
    }

    private void OnConnectResult(bool ok)
    {
        if( ! ok )
        {
            Debug.LogError( "OnConnectResult," + ok );
            return;
        }
        var login = new TestSvr.Login();
        login.Name = "圆球";
        login.ServerFd = 0;
        ushort main=1;
        ushort sub=1;
        Assets.ClientNetwork.sendMsg(main,sub,login);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            myAgent.SetDestination(new Vector3(3,0.5f));
        }
        if(0>fdServerSocket)
        {
            Assets.ClientNetwork.ConnectServer("196.168.1.115", 8999);
            fdServerSocket = 0;
        }
    }
}
